package com.vshop.controller.partner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.comentity.PartnerInfo;
import com.vshop.entity.interfacerequest.ExceptionRequest;
import com.vshop.entity.interfacerequest.partnershowinfo.request.RequestPartnerShowInfo;
import com.vshop.entity.interfacerequest.partnershowinfo.request.RequestPartnerShowInfoData;
import com.vshop.entity.interfacerequest.partnershowinfo.response.ResponsePartnerShowInfo;
import com.vshop.entity.interfacerequest.partnershowinfo.response.ResponsePartnerShowInfoData;
import com.vshop.entity.interfacerequest.partnershowinfo.response.ResponsePartnerShowInfoTradeData;
import com.vshop.service.PartnerInfoService;
import com.vshop.service.SequenceService;
import com.vshop.utils.Md5Encrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 获取归属企业展示信息
 *
 * @author lm
 * @date 2019/03/07
 */
@RestController
@Api(description = "获取归属企业展示信息")
@Slf4j
public class PartnerShowInfoController {
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private PartnerInfoService partnerInfoService;

    @CrossOrigin
    @PostMapping(value = "partnerShowInfo", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取归属企业展示信息", response = ResponsePartnerShowInfo.class)
    public String partnerShowInfo(@ApiParam @RequestBody RequestPartnerShowInfo request) {
        // 1，生成流水号
        String connSeqNo = sequenceService.getPackageTaskNo();
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());
        //3,定义要回复的内容
        String jsonString;
        ExceptionRequest utils = new ExceptionRequest();
        String requestStr = JSON.toJSONString(request);
        if (requestStr == null) {
            log.info("请求报文不合法:requestStr为空");
            jsonString = utils.dataWeiKong(connSeqNo, curtime);
            return jsonString;
        } else {
            log.info("前端请求报文是：" + requestStr);
            RequestPartnerShowInfo requestPackage = JSONArray.parseObject(requestStr, RequestPartnerShowInfo.class);
            String requestSign = requestPackage.getSign();
            if (requestPackage.getSign() == null) {
                log.info("请求报文不合法:sign为空");
                jsonString = utils.dataWeiKong(connSeqNo, curtime);
                return jsonString;
            } else {
                log.info("请求的签名是：" + requestSign);
                RequestPartnerShowInfoData requestData = requestPackage.getData();
                if (requestPackage.getData() == null) {
                    log.info("请求报文不合法:data为空");
                    jsonString = utils.dataWeiKong(connSeqNo, curtime);
                    return jsonString;
                } else {
                    String ConnSeqNo = requestData.getConnSeqNo();
                    String partnerCode = requestData.getPartnerCode();
                    //String token = requestData.getToken();
                    String tradeTime = requestData.getTradeTime();
                    String tradeType = requestData.getTradeType();
                    String lang = requestData.getLang();
                    String version = requestData.getVersion();

                    if (StringUtils.isBlank(ConnSeqNo) || StringUtils.isBlank(partnerCode) || StringUtils.isBlank(tradeTime) || StringUtils.isBlank(tradeType) || lang == null || version == null) {
                        log.info("请求报文不合法:data参数不合法");
                        jsonString = utils.dataWeiKong(connSeqNo, curtime);
                        return jsonString;
                    } else {
                        // 将请求体json转成String
                        String str = JSON.toJSONString(requestData);
                        // 将Str拿去计算签名
                        String sign = utils.calculateSignature(str);
                        log.info("计算的签名是：" + sign);
                        if (!requestSign.equalsIgnoreCase(sign)) {
                            log.info("签名验证失败！");
                            jsonString = utils.signatureFailure(ConnSeqNo, tradeTime, tradeType, "0001", "签名有误，请确认签名！");
                            return jsonString;
                        } else {//签名验证通过
                            log.info("签名验证通过");
                            //用partnerCode去数据库查询数据表。
                            List<PartnerInfo> partnerInfoList = partnerInfoService.queryLogoByCode(partnerCode);
                            ResponsePartnerShowInfoTradeData tradeData = null;
                            if (partnerInfoList != null && !partnerInfoList.isEmpty()) {
                                for (PartnerInfo p : partnerInfoList) {
                                    String partnerName = p.getPartnerName();
                                    String companylogo = p.getCompanylogo();

                                    tradeData = new ResponsePartnerShowInfoTradeData();
                                    tradeData.setCompanylogo(companylogo);
                                    tradeData.setCompanyName(partnerName);
                                }

                                ResponsePartnerShowInfoData data = new ResponsePartnerShowInfoData();
                                data.setConnSeqNo(ConnSeqNo);
                                data.setPartnerCode(partnerCode);
                                data.setTradeType(tradeType);
                                data.setTradeTime(tradeTime);
                                data.setTradeRstCode("0000");
                                if (lang.equals("cn") || StringUtils.isBlank(lang)) {
                                    data.setTradeRstMessage("成功");
                                } else {
                                    data.setTradeRstMessage("success");
                                }
                                data.setTradeData(tradeData);

                                ResponsePartnerShowInfo responsePartnerShowInfo = new ResponsePartnerShowInfo();
                                responsePartnerShowInfo.setData(data);
                                responsePartnerShowInfo.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                jsonString = JSON.toJSONString(responsePartnerShowInfo);
                                log.info("响应报文：" + jsonString);
                                return jsonString;

                            } else {

                                log.info("接口返回出现问题");

                                ResponsePartnerShowInfoData data = new ResponsePartnerShowInfoData();
                                data.setConnSeqNo(ConnSeqNo);
                                data.setPartnerCode(partnerCode);
                                data.setTradeType(tradeType);
                                data.setTradeTime(tradeTime);
                                data.setTradeRstCode("0004");
                                if (lang.equals("cn") || StringUtils.isBlank(lang)) {
                                    data.setTradeRstMessage("未查询到相应数据");
                                } else {
                                    data.setTradeRstMessage("No corresponding data was queried！");
                                }
                                data.setTradeData(tradeData);

                                ResponsePartnerShowInfo responsePartnerShowInfo = new ResponsePartnerShowInfo();
                                responsePartnerShowInfo.setData(data);
                                responsePartnerShowInfo.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                jsonString = JSON.toJSONString(responsePartnerShowInfo);
                                log.info("响应报文：" + jsonString);
                                return jsonString;

                            }
                        }
                    }
                }
            }
        }

    }
}
